home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 6 / FM Towns Free Software Collection 6.iso / ms_dos / happysrc / picallsp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-08  |  25.1 KB  |  832 lines

  1. /**********************************************************************
  2.  *
  3.  *       *** HAPPy P-code Interpriter ***
  4.  *
  5.  *          Standard procedure , function の処理
  6.  *
  7.  *        void callsp(void)
  8.  *        void T_get(int fileno, _store *filebuf, FILE *fp,char *name)
  9.  *
  10.  *            Copyright (c) H.Asano 1992.
  11.  **********************************************************************/
  12.  
  13. #define EXTERN extern
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <math.h>
  19. #include "hapai.h"
  20.  
  21. extern void prerr(int,char*) ;
  22.  
  23. /********* P-CODE COMPUTER store **********/
  24. extern _store store[] ;
  25.  
  26. /********* P-CODE COMPUTER registers **********/
  27.  
  28. extern int q   ;      /* q */
  29.  
  30. extern _store *sp ;  /* sp points to top of the stack */
  31.  
  32. extern int pc   ;     /* program counter */
  33. extern int mp   ;     /* mp points to begginning of a data segment */
  34. extern int ep   ;     /* ep points to the maxmum extent of the stack */
  35. extern int np   ;     /* np points to top of the dynamically allocated area */
  36.  
  37.  
  38. extern boolean readlnflag ;             /* inputにreadlnをした時 及び
  39.                                            起動時には真               */
  40.  
  41.  
  42. /**************************************************/
  43. /* searchfile() : fileadrに対応するファイルを探す *
  44. /**************************************************/
  45. static int searchfile(int fileno)
  46. {
  47.   register int i = -1 ;
  48.  
  49.      while(fi[++i].fileadr != fileno) ;
  50.      return(i) ;
  51. }
  52.  
  53. /**************************************************/
  54. /* fileerrmsg() : file関係のエラーメッセージ出力  */
  55. /**************************************************/
  56. static void fileerrmsg(int msgno,char *pname,int fileno,char *msg)
  57. {                                       
  58.   char *fname, *rfname;
  59.   char buf[160] ;
  60.   
  61.       fname =  fi[fileno].filename ;
  62.       rfname = fi[fileno].rfname   ; 
  63.       sprintf(buf,"%s: %s(実ファイル=%s)%s", pname,fname,rfname,msg) ;
  64.       prerr(msgno,buf) ;
  65. }
  66.  
  67. /*****************************************/
  68. /* T_get() : textファイルからの1文字読込 */
  69. /*****************************************/
  70. void T_get(int fileno, _store *filebuf, FILE *fp,char *name)
  71. {
  72.      if(feof(fi[fileno].fp))
  73.       fileerrmsg(42,name,fileno,"がEOFとなっている") ;
  74.  
  75.      (*filebuf).vc = getc(fp) ;         /* 1文字先読み                */
  76.  
  77.      if(ferror(fp))
  78.       fileerrmsg(181,name,fileno,"で障害が発生した") ;
  79.       
  80.      fi[fileno].eolnflag = (*filebuf).vc == '\n' ; 
  81.                                         /* 改行を読んだ時 真          */
  82.      if(fi[fileno].eolnflag) (*filebuf).vc = ' ' ;
  83. }
  84.  
  85. /******************* 各P-codeの処理 ***********************************/
  86.  
  87. /**************************************/
  88. /* ATN() : arctan標準関数             */
  89. /**************************************/
  90. static void ATN(void)
  91. {
  92.      (*sp).vr = (float)atan((double)(*sp).vr);
  93. }
  94.  
  95. /**************************************/
  96. /* COS() : cos標準関数                */
  97. /**************************************/
  98. static void COS(void)
  99. {
  100.      (*sp).vr = (float)cos((double)(*sp).vr) ;
  101. }
  102.  
  103. /**************************************/
  104. /* DIS() : dispose標準手続き          */
  105. /**************************************/
  106. static void DIS(void)
  107. {
  108.   int ad ;
  109.  
  110.      ad = (*(sp-1)).va ;                /* 解放するアドレス           */
  111.      if(ad == NilValue)
  112.       prerr(23,"dispose: 引数の値がnilである") ;
  113.      if((np <= ad) && (ad < Maxstore)) {            /* 正常値         */
  114.       if(ad == np) np += (short)(*sp).vi ;    /* 一番後にnewした時だけ*/
  115.                                               /* 本当に解放する       */
  116.      }
  117.      else prerr(24,"dispose: 引数の値が不定である") ;
  118.      sp-=2 ;
  119. }
  120.  
  121. /***************************************/
  122. /* EOL() : eol標準関数                 */
  123. /***************************************/
  124. static void EOL(void)
  125. {
  126.   int fileno ;
  127.  
  128.      fileno  = searchfile((*sp).va) ;
  129.  
  130.      if(fi[fileno].mode == undefined)
  131.       fileerrmsg(41,"eoln",fileno,"が不定である") ;
  132.  
  133.      if(feof(fi[fileno].fp))
  134.       fileerrmsg(42,"eoln",fileno,"がEOFとなっている") ;
  135.  
  136.      if((fileno==0) && readlnflag) {    /* inputファイルで直前がreadln*/
  137.       T_get(fileno,store+(*sp).va,fi[fileno].fp,"eoln") ;
  138.                                         /* 次の文字を読込             */
  139.       readlnflag = false ;
  140.      } 
  141.      (*sp).vb = fi[fileno].eolnflag ;
  142. }
  143.  
  144. /***************************************/
  145. /* EoF() : eof標準関数                 */
  146. /***************************************/
  147. static void EoF(void)
  148. {
  149.   int fileadr ;
  150.   int fileno  ;
  151.  
  152.      fileadr = (*sp).va ;
  153.      fileno  = searchfile(fileadr) ;
  154.      if((fileno==0) && readlnflag) {    /* inputファイルで直前がreadln*/
  155.       T_get(fileno,store+fileadr,fi[fileno].fp,"eof") ;
  156.                                         /* 次の文字を読込             */
  157.       readlnflag = false ;
  158.       (*sp).vb = feof(fi[fileno].fp) ;
  159.      }
  160.      else {                             /* 通常のeof                  */
  161.       if(fi[fileno].mode == undefined)
  162.        fileerrmsg(40,"eof",fileno,"が不定である") ;
  163.  
  164.       (*sp).vb = (fi[fileno].mode == generation) /* 生成モードは真*/
  165.                      ?  true : feof(fi[fileno].fp) ;
  166.      }
  167. }
  168.  
  169. /**************************************/
  170. /* EXP() : exp標準関数                */
  171. /**************************************/
  172. static void EXP(void)
  173. {
  174.      (*sp).vr = (float)exp((double)(*sp).vr) ;
  175. }
  176.  
  177. /**************************************/
  178. /* GET() : テキストファイル以外のget  */
  179. /**************************************/
  180. static void GET(void)
  181. {
  182.   int fileadr ;
  183.   int fileno  ;
  184.   FILE *fp    ;
  185.  
  186.      fileadr = (*sp--).va ;
  187.      fileno  = searchfile(fileadr) ;
  188.      fp      = fi[fileno].fp ;
  189.  
  190.      if(fi[fileno].mode != inspection)
  191.       fileerrmsg(14,"get",fileno,"が検査モードでない") ;
  192.  
  193.      if(feof(fp))
  194.       fileerrmsg(16,"get",fileno,"がEOFとなっている") ;
  195.  
  196.      fread(store+fileadr, fi[fileno].filesize*sizeof(_store),1,fp);
  197.  
  198.      if(ferror(fp))
  199.       fileerrmsg(131,"get",fileno,"で障害が発生した") ;
  200. }
  201.  
  202. /**************************************/
  203. /* LOG() : ln標準関数                 */
  204. /**************************************/
  205. static void LOG(void)
  206. {
  207.      if((*sp).vr <= (float)0.0)
  208.       prerr(33,"ln: 引数の値が0以下である") ;
  209.      (*sp).vr = (float)log((double)(*sp).vr);
  210. }
  211.  
  212. /**************************************/
  213. /* NEW() : new標準手続き              */
  214. /**************************************/
  215. static void NEW(void)
  216. {
  217.   int ad ;
  218.  
  219.      ad = np-(short)(*sp).vi ;
  220.      if(ad <= ep) 
  221.        prerr(121,"new: メモリ不足で割り付けができない") ;
  222.      np = ad ;
  223.      ad = (*(sp-1)).va ;
  224.      store[ad].va = np ;
  225.      sp-=2 ;
  226. }
  227.  
  228. /**************************************/
  229. /* PGE() : page標準手続き             */
  230. /**************************************/
  231. static void PGE(void)
  232. {
  233.   int fileno ;
  234.  
  235.      fileno = searchfile((*sp--).va) ;
  236.      if(fi[fileno].mode != generation)
  237.       fileerrmsg(9,"page",fileno,"が生成モードでない") ;
  238.  
  239.      if(!fi[fileno].writelnflag)        /* 最後が改行でない時         */
  240.       fputc('\n',fi[fileno].fp) ;       /*   改行を付ける             */
  241.      fputc('\f',fi[fileno].fp)  ;       /* ホームフィード             */ 
  242.      fi[fileno].writelnflag = false ; 
  243. }
  244.  
  245. /**************************************/
  246. /* PUT() : テキストファイル以外のput  */
  247. /**************************************/
  248. static void PUT(void)
  249. {
  250.   int  fileadr ;
  251.   int  fileno  ;
  252.   FILE *fp     ;
  253.  
  254.      fileadr = (*sp--).va ;
  255.      fileno  = searchfile(fileadr) ;
  256.      fp      = fi[fileno].fp  ;
  257.  
  258.      if(fi[fileno].mode != generation)
  259.       fileerrmsg(9,"put",fileno,"が生成モードでない") ;
  260.  
  261.      fwrite(store+fileadr, fi[fileno].filesize*sizeof(_store),1,fp) ;
  262.  
  263.      if(ferror(fp))
  264.       fileerrmsg(182,"put",fileno,"で障害が発生した") ;
  265. }
  266.  
  267. /**************************************/
  268. /* RLN() : テキストファイルのreadln   */
  269. /**************************************/
  270. static void RLN(void)
  271. {
  272.   FILE   *fp      ;
  273.   _store *filebuf ;
  274.   int  fileno ;
  275.  
  276.      fileno  = searchfile((*sp).va) ;
  277.      fp      = fi[fileno].fp            ;
  278.      filebuf = fi[fileno].filebuf       ;
  279.  
  280.      if(fi[fileno].mode != inspection)
  281.       fileerrmsg(14,"read",fileno,"が検査モードでない") ;
  282.  
  283.      if((fileno==0) && readlnflag)
  284.       T_get(fileno,filebuf,fp,"readln");
  285.  
  286.      while(!fi[fileno].eolnflag)
  287.       T_get(fileno,filebuf,fp,"readln");  /* 改行文字を読み飛ばす     */
  288.      if(fileno == 0) readlnflag = true ;
  289.      else 
  290.       T_get(fileno,filebuf,fp,"readln") ; /* 次の文字を読込           */
  291.  
  292.      sp-- ;
  293. }
  294.  
  295. /*****************************************/
  296. /* RDC() : テキストファイルからの文字読込*/
  297. /*****************************************/
  298. static void RDC(void)
  299. {
  300.   FILE   *fp      ;
  301.   _store *filebuf ;
  302.   int  fileno ;
  303.   short ad ;
  304.   
  305.      fileno  = searchfile((*sp).va) ;
  306.      fp      = fi[fileno].fp            ;
  307.      filebuf = fi[fileno].filebuf       ;
  308.  
  309.      if(fi[fileno].mode != inspection)
  310.       fileerrmsg(14,"read",fileno,"が検査モードでない") ;
  311.  
  312.      if(feof(fp))
  313.       fileerrmsg(16,"read",fileno,"がEOFとなっている") ;
  314.  
  315.      if((fileno==0) && readlnflag) {    /* inputファイルで直前がreadln*/
  316.       T_get(fileno,filebuf,fp,"read") ; /* 次の文字を読込             */
  317.       readlnflag = false ;
  318.      }
  319.  
  320.      ad = (*(sp-1)).va ;
  321.      store[ad].vc = (*filebuf).vc  ;    /* バッファ変数から変数へ代入 */
  322.  
  323.      T_get(fileno,filebuf,fp,"read") ;  /* 次の文字を読込             */
  324.  
  325.      sp-=2               ;
  326. }
  327.  
  328. /*****************************************/
  329. /* RDI() : テキストファイルからの整数読込*/
  330. /*****************************************/
  331. static void RDI(void)
  332. {
  333.   FILE   *fp      ;
  334.   _store *filebuf ;
  335.   int  fileno ;
  336.   int  ad  ;
  337.   int  lch ;
  338.   integer  ival = 0 ;
  339.   int sign = 1 ;
  340.       
  341.      fileno  = searchfile((*sp).va) ;
  342.      fp      = fi[fileno].fp            ;
  343.      filebuf = fi[fileno].filebuf       ;
  344.      ad = (*(sp-1)).va;
  345.  
  346.      if(fi[fileno].mode != inspection)
  347.       fileerrmsg(14,"read",fileno,"が検査モードでない") ;
  348.  
  349.      if((fileno==0) && readlnflag) {    /* inputファイルで直前がreadln*/
  350.       T_get(fileno,filebuf,fp,"read") ; /* 次の文字を読込             */
  351.       readlnflag = false ;
  352.      }
  353.  
  354.      while((*filebuf).vc == ' ')        /* 空白読み飛ばし             */
  355.       T_get(fileno,filebuf,fp,"read");
  356.      if(((*filebuf).vc=='+') || ((*filebuf).vc=='-')) { /* 符号の時   */
  357.       sign = ((*filebuf).vc=='+') ? 1 : -1  ; /* 符号に応じた正負     */
  358.       T_get(fileno,filebuf,fp,"read");
  359.      }
  360.  
  361.      if(((*filebuf).vc >= '0') && ((*filebuf).vc <= '9')) { /* 数字   */
  362.       do {
  363.        lch = (*filebuf).vc - '0' ;
  364.        if(ival <= (Maxint-lch/10))      /* 上限チェック               */
  365.         ival = ival*10 + lch ;
  366.        else
  367.         fileerrmsg(54,"read",fileno,
  368.                    "から符号付き整数の形式でないものを読もうとした") ;
  369.        T_get(fileno,filebuf,fp,"read");
  370.        if(feof(fp)) break ;             /* eofだったら終わり          */
  371.       } while(('0' <= (*filebuf).vc ) && ((*filebuf).vc <= '9')) ;
  372.  
  373.       store[ad].vi = sign * ival ;
  374.      }
  375.      else {                             /* 最初に数字がこなかった時   */
  376.       if(feof(fp)) fileerrmsg(16,"read",fileno,"がEOFとなっている") ;
  377.       fileerrmsg(54,"read",fileno,
  378.                  "から符号付き整数の形式でないものを読もうとした") ;
  379.      }
  380.  
  381.      sp-=2;
  382. }
  383.  
  384. /*****************************************/
  385. /* RDR() : テキストファイルからの実数読込*/
  386. /*****************************************/
  387. static void RDR(void)
  388. {
  389.   FILE   *fp      ;
  390.   _store *filebuf ;
  391.   int  fileno ;
  392.   char buf[80] ;
  393.   char *stopstring ;
  394.   int  i       ;
  395.   integer ival = 0 ;                    /* 整数部の値                 */
  396.  
  397.      fileno  = searchfile((*sp).va) ;
  398.      fp      = fi[fileno].fp            ;
  399.      filebuf = fi[fileno].filebuf       ;
  400.  
  401.      if(fi[fileno].mode != inspection)
  402.       fileerrmsg(14,"read",fileno,"が検査モードでない") ;
  403.  
  404.      if((fileno==0) && readlnflag) {    /* inputファイルで直前がreadln*/
  405.       T_get(fileno,filebuf,fp,"read") ; /* 次の文字を読込             */
  406.       readlnflag = false ;
  407.      }
  408.  
  409.      for(i=0;i<80;i++) *(buf+i) = '\0'; /* バッファクリア             */
  410.      i = 0 ;
  411.  
  412.      while((*filebuf).vc == ' ')        /* 空白読み飛ばし             */
  413.       T_get(fileno,filebuf,fp,"read");
  414.      if(((*filebuf).vc=='+') || ((*filebuf).vc=='-')) { /* 符号の時   */
  415.       buf[i++] = (char)(*filebuf).vc ;
  416.       T_get(fileno,filebuf,fp,"read");
  417.      }
  418.  
  419.      if(((*filebuf).vc >= '0') && ((*filebuf).vc <= '9')) { /* 数字   */
  420.       do {
  421.        buf[i++] = (char)(*filebuf).vc ;
  422.        ival = ival*10 + (*filebuf).vc - '0';
  423.        T_get(fileno,filebuf,fp,"read");
  424.        if(feof(fp)) break ;             /* eofだったら終わり          */
  425.       } while(('0' <= (*filebuf).vc ) && ((*filebuf).vc <= '9')) ;
  426.  
  427.       if((*filebuf).vc == '.') {         /* 小数点                     */
  428.        buf[i++] = (char)(*filebuf).vc ;
  429.        T_get(fileno,filebuf,fp,"read");  /* 小数点を読み飛ばす         */
  430.        if(((*filebuf).vc >= '0') && ((*filebuf).vc <= '9')) { /* 数字  */
  431.         do {
  432.          buf[i++] = (char)(*filebuf).vc ;
  433.          T_get(fileno,filebuf,fp,"read");
  434.          if(feof(fp)) break ;           /* eofだったら終わり          */
  435.         } while(('0' <= (*filebuf).vc ) && ((*filebuf).vc <= '9')) ;
  436.        }
  437.        else goto error ;                /* . の次が数字でない時       */
  438.       }
  439.  
  440.       if(((*filebuf).vc == 'e') || ((*filebuf).vc == 'E')) { /* 指数*/
  441.        buf[i++] = (char)(*filebuf).vc ;
  442.        T_get(fileno,filebuf,fp,"read");  /* eまたはEを読み飛ばす    */
  443.        if(((*filebuf).vc=='+') || ((*filebuf).vc=='-')) { /* 符号の時*/
  444.         buf[i++] = (char)(*filebuf).vc ;
  445.         T_get(fileno,filebuf,fp,"read");
  446.        }
  447.        if(((*filebuf).vc >= '0') && ((*filebuf).vc <= '9')) { /* 数字*/
  448.         do {
  449.          buf[i++] = (char)(*filebuf).vc ;
  450.          T_get(fileno,filebuf,fp,"read");
  451.          if(feof(fp)) break ;           /* eofだったら終わり          */
  452.         } while(('0' <= (*filebuf).vc ) && ((*filebuf).vc <= '9')) ; 
  453.        }
  454.        else goto error ;                /* 指数部の最初が数字でない   */
  455.       }
  456.      }
  457.      else goto error ;                  /* 最初に数字がこなかった時   */
  458.  
  459.      store[(*(sp-1)).va].vr = (float)strtod(buf,&stopstring) ;
  460.  
  461.      sp -= 2;
  462.      return ;
  463.  
  464.  error :                                /* 符号つき数字でない時       */
  465.      if(feof(fp)) fileerrmsg(16,"read",fileno,"がEOFとなっている") ;
  466.      fileerrmsg(56,"read",fileno,
  467.                 "から符号付き数の形式でないものを読もうとした") ;
  468. }
  469.  
  470. /****************************************/
  471. /* RST() : テキストファイル以外のreset  */
  472. /****************************************/
  473. static void RST(void)
  474. {
  475.    int  fileadr ;
  476.    int  fileno  ;
  477.    FILE *fp     ;
  478.  
  479.      fileadr = (*sp--).va ;
  480.      fileno  = searchfile(fileadr) ;
  481.  
  482.      fclose(fi[fileno].fp) ;            /*まずクローズ(エラーでも良い)*/
  483.      fp = fopen(fi[fileno].rfname,"rb") ;
  484.      if(fp == NULL)
  485.       fileerrmsg(13,"reset",fileno,"が不定である") ;
  486.  
  487.      fi[fileno].mode = inspection ;     /* 検査モード                 */
  488.      fi[fileno].fp   = fp ;
  489.      fread(store+fileadr, fi[fileno].filesize*sizeof(_store),1,fp) ;
  490.                                         /* 最初の要素をバッファ変数に */
  491. }
  492.  
  493. /******************************************/
  494. /* RWT() : テキストファイル以外のrewrite  */
  495. /******************************************/
  496. static void RWT(void)
  497. {
  498.   int  fileno  ;
  499.  
  500.      fileno  = searchfile((*sp--).va) ;
  501.  
  502.      fclose(fi[fileno].fp) ;            /*まずクローズ(エラーでも良い)*/
  503.      fi[fileno].fp = fopen(fi[fileno].rfname,"wb") ;
  504.      if(fi[fileno].fp == NULL)
  505.       fileerrmsg(132,"rewrite",fileno,"のオープンができない") ;
  506.  
  507.      fi[fileno].mode = generation ;     /* 生成モード                 */
  508. }
  509.  
  510. /***************************************/
  511. /* SIN() : sin標準関数                 */
  512. /***************************************/
  513. static void SIN(void)
  514. {
  515.      (*sp).vr = (float)sin((double)(*sp).vr) ;
  516. }
  517.  
  518. /***************************************/
  519. /* SQT() : sqrt標準関数                */
  520. /***************************************/
  521. static void SQT(void)
  522. {
  523.      if((*sp).vr < (float)0.0)      /* 負の平方根                 */
  524.       prerr(34,"sqrt:引数の値が負である") ;
  525.      (*sp).vr = (float)sqrt((double)(*sp).vr);
  526. }
  527.  
  528. /***************************************/
  529. /* TGT() : テキストファイルのget       */
  530. /***************************************/
  531. static void TGT(void)
  532. {
  533.   FILE   *fp      ;
  534.   _store *filebuf ;
  535.   int  fileno ;
  536.  
  537.      fileno  = searchfile((*sp).va) ;
  538.      fp      = fi[fileno].fp            ;
  539.      filebuf = fi[fileno].filebuf       ;
  540.  
  541.      if(fi[fileno].mode != inspection)
  542.       fileerrmsg(14,"get",fileno,"が検査モードでない") ;
  543.  
  544.      if((fileno==0) && readlnflag) {    /* inputファイルで直前がreadln*/
  545.       T_get(fileno,filebuf,fp,"get") ;  /* 次の文字を読込             */
  546.       readlnflag = false ;
  547.      }
  548.  
  549.      if(feof(fp))
  550.       fileerrmsg(16,"get",fileno,"がEOFとなっている") ;
  551.  
  552.      T_get(fileno,filebuf,fp,"get") ;   /* 次の文字を読込             */
  553.  
  554.      sp-- ;
  555. }
  556.  
  557. /***************************************/
  558. /* TPT() : テキストファイルへのput     */
  559. /***************************************/
  560. static void TPT(void)
  561. {
  562.   FILE *fp    ;
  563.   int  fileno ;
  564.  
  565.      fileno  = searchfile((*sp).va) ;
  566.      fp      = fi[fileno].fp            ;
  567.      if(fi[fileno].mode != generation)
  568.       fileerrmsg(9,"put",fileno,"が生成モードでない") ;
  569.  
  570.      if(fputc((*(store+(*sp--).va)).vc,fp) == EOF)   /* 文字出力  */
  571.       fileerrmsg(131,"put",fileno,"で障害が発生した") ;
  572.      fi[fileno].writelnflag = false ; 
  573. }
  574.  
  575. /***************************************/
  576. /* TRS() : テキストファイルへのreset   */
  577. /***************************************/
  578. static void TRS(void)
  579. {
  580.   FILE *fp ;
  581.   int  fileno ;
  582.   _store *filebuf ;
  583.  
  584.      fileno  = searchfile((*sp--).va) ;
  585.      filebuf = fi[fileno].filebuf         ;
  586.  
  587.      if((strcmp(fi[fileno].filename,"input")==0) || /* inputファイル  */
  588.         (strcmp(fi[fileno].filename,"output")==0))  /* outputファイル */
  589.       fileerrmsg(81,"reset",fileno,"に対してresetはできない") ;
  590.  
  591.      if(fi[fileno].mode == generation)  /* 今が生成モードの時         */
  592.       if(! fi[fileno].writelnflag)      /* 最後が改行マークでない時   */
  593.        fputc('\n',fi[fileno].fp) ;      /* 改行を書き込む             */
  594.      fclose(fi[fileno].fp) ;            /* ファイルクローズ           */
  595.      fp = fopen(fi[fileno].rfname,"rt") ; /* テキストモードオープン   */
  596.      if(fp == NULL) 
  597.       fileerrmsg(13,"reset",fileno,"が不定である") ;
  598.      fi[fileno].fp   = fp ;
  599.      fi[fileno].mode = inspection ;     /* 検査モード                 */
  600.      fi[fileno].textfile = true   ;     /* テキストファイル           */
  601.  
  602.      T_get(fileno,filebuf,fp,"reset") ; /* 最初の文字を読む           */
  603. }
  604.  
  605. /***************************************/
  606. /* TRW() : テキストファイルへのrewrite */
  607. /***************************************/
  608. static void TRW(void)
  609. {
  610.   int fileno ;
  611.  
  612.      fileno  = searchfile((*sp--).va) ;
  613.  
  614.      if((strcmp(fi[fileno].filename,"input")==0) || /* inputファイル  */
  615.         (strcmp(fi[fileno].filename,"output")==0))  /* outputファイル */
  616.       fileerrmsg(82,"rewrite",fileno,"に対してrewriteはできない") ;
  617.  
  618.      fclose(fi[fileno].fp) ;            /*まずクローズ(エラーでも良い)*/
  619.      fi[fileno].fp = fopen(fi[fileno].rfname,"wt") ;
  620.      if(fi[fileno].fp == NULL)
  621.       fileerrmsg(132,"rewrite",fileno,"のオープンができない") ;
  622.  
  623.      fi[fileno].mode = generation ;     /* 生成モード                 */
  624.      fi[fileno].textfile = true   ;     /* テキストファイル           */
  625.      fi[fileno].writelnflag = true;     /* rewrite直後には改行いらない*/
  626. }
  627.  
  628. /***************************************/
  629. /* WLN() : writeln標準手続き           */
  630. /***************************************/
  631. static void WLN(void)
  632. {
  633.   int fileno ;
  634.  
  635.      fileno = searchfile((*sp--).va) ;
  636.      if(fi[fileno].mode != generation)
  637.       fileerrmsg(9,"writeln",fileno,"が生成モードでない") ;
  638.      fputc('\n',fi[fileno].fp) ;
  639.      fi[fileno].writelnflag = true ; 
  640. }
  641.  
  642. /***************************************/
  643. /* WRB() : boolean型の出力             */
  644. /***************************************/
  645. static void WRB(void)
  646. {
  647.   int fileno ;
  648.   char *put ;
  649.   short i,j,k ;
  650.   FILE *fp  ;
  651.  
  652.      fileno = searchfile((*sp).va) ;
  653.      fp = fi[fileno].fp ;
  654.  
  655.      if(fi[fileno].mode != generation)
  656.       fileerrmsg(9,"write",fileno,"が生成モードでない") ;
  657.  
  658.      put = ((*(sp-2)).vb) ?  "TRUE" : "FALSE" ;
  659.      j   = strlen(put) ;                /* 出力文字長                 */
  660.      k = (short)(*(sp-1)).vi ;          /* 出力幅                     */
  661.  
  662.      if(k>j)                            /* 空白をつける必要のある時   */
  663.       for(i=1;i<=(k-j);i++) fputc(' ',fp) ;
  664.      else j= k ;
  665.      for(i=0;i<j;i++) fputc((int)put[i], fp);
  666.  
  667.      fi[fileno].writelnflag = false ; 
  668.  
  669.      sp-=3;
  670. }
  671.  
  672. /***************************************/
  673. /* WRC() : テキストファイルに文字出力  */
  674. /***************************************/
  675. static void WRC(void)
  676. {
  677.   int fileno ;
  678.   int i ;
  679.   FILE *fp  ;
  680.  
  681.      fileno = searchfile((*sp).va) ;
  682.      fp = fi[fileno].fp ;
  683.  
  684.      if(fi[fileno].mode != generation)
  685.       fileerrmsg(9,"write",fileno,"が生成モードでない") ;
  686.  
  687.      for(i=1;i<(int)(*(sp-1)).vi;i++) fputc(' ',fp) ;
  688.      fputc((*(sp-2)).vc,fp) ;    
  689.  
  690.      fi[fileno].writelnflag = false ; 
  691.  
  692.      sp-=3 ;
  693. }
  694.  
  695. /***************************************/
  696. /* WRF() : テキストファイルに実数出力  */
  697. /***************************************/
  698. static void WRF(void)
  699. {
  700.   int fileno ;
  701.   FILE *fp  ;
  702.   char buf1[80] ;
  703.   char buf2[80] ;
  704.   int width ;
  705.   int i ;
  706.  
  707.      fileno = searchfile((*sp).va) ;
  708.      fp = fi[fileno].fp ;
  709.  
  710.      if(fi[fileno].mode != generation)
  711.       fileerrmsg(9,"write",fileno,"が生成モードでない") ;
  712.  
  713.      *buf1 = '%' ;                      /*  %1.xfという形を生成       */
  714.      sprintf(buf1+1,"1.%df",(int)(*(sp-1)).vi) ;   /* 少数点以下の桁数*/
  715.      width = sprintf(buf2,buf1,(*(sp-3)).vr);      /* 固定小数点変換  */  
  716.      for(i=width;i<(int)(*(sp-2)).vi;i++)
  717.       fputc(' ',fp) ;                   /* 前に空白を出力する         */
  718.      fputs(buf2,fp) ;
  719.  
  720.      fi[fileno].writelnflag = false ; 
  721.  
  722.      sp-=4 ;
  723. }
  724.  
  725. /***************************************/
  726. /* WRI() : テキストファイルに整数出力  */
  727. /***************************************/
  728. static void WRI(void)
  729. {
  730.   char buf[12];
  731.   int fileno ;
  732.   FILE *fp  ;
  733.   short i,len ;
  734.  
  735.      fileno = searchfile((*sp).va) ;
  736.      fp = fi[fileno].fp ;
  737.  
  738.      if(fi[fileno].mode != generation)
  739.       fileerrmsg(9,"write",fileno,"が生成モードでない") ;
  740.  
  741.       len=sprintf(buf,"%ld",(*(sp-2)).vi); /* 数字出力に必要な桁 */
  742.       for(i=(short)(*(sp-1)).vi;i>len;i--)
  743.        fputc(' ',fp) ;                  /* 数字の前に空白を出力する   */
  744.       fputs(buf,fp)  ;
  745.  
  746.      fi[fileno].writelnflag = false ; 
  747.  
  748.      sp -= 3 ;      
  749. }
  750.  
  751. /***************************************/
  752. /* WRR() : テキストファイルに実数出力  */
  753. /***************************************/
  754. static void WRR(void)
  755. {
  756.   char buf[15] ;
  757.   int keta     ;
  758.   int width    ;
  759.   int point    ;
  760.   int fileno ;
  761.   FILE *fp  ;
  762.   
  763.      fileno = searchfile((*sp).va) ;
  764.      fp = fi[fileno].fp ;
  765.  
  766.      if(fi[fileno].mode != generation)
  767.       fileerrmsg(9,"write",fileno,"が生成モードでない") ;
  768.  
  769.      keta  = (int)(*(sp-1)).vi    ;     /* 出力桁                     */
  770.      width = (keta >= 9) ? keta : 9 ;   /* 必要出力桁                 */
  771.      point = width - 8 ;
  772.      *buf = '%' ;                       /* %x.xEの形に変換            */
  773.      sprintf(buf+1,"%d.%dE",width,point);
  774.      fprintf(fp,buf,(*(sp-2)).vr) ;
  775.  
  776.      fi[fileno].writelnflag = false ; 
  777.  
  778.      sp -= 3 ;
  779. }
  780.  
  781. /***************************************/
  782. /* WRS() : テキストファイルに文字列出力*/
  783. /***************************************/
  784. static void WRS(void)
  785. {
  786.   int fileno ;
  787.   FILE *fp  ;
  788.   short i,j,k ;
  789.   int ad    ;
  790.  
  791.      fileno = searchfile((*sp).va) ;
  792.      fp = fi[fileno].fp ;
  793.  
  794.      if(fi[fileno].mode != generation)
  795.       fileerrmsg(9,"write",fileno,"が生成モードでない") ;
  796.  
  797.      ad=        (*(sp-3)).va ;
  798.      k = (short)(*(sp-2)).vi ;          /* k=出力幅                   */
  799.      j = (short)(*(sp-1)).vi ;          /* j=文字数                   */
  800.  
  801.      if(k>j)                            /* 空白をつける必要のある時   */
  802.       for(i=1;i<=(k-j);i++) fputc(' ',fp) ;
  803.      else j= k ;
  804.      for(i=0;i<j;i++)
  805.       fputc(store[ad+i].vc,fp);
  806.  
  807.      fi[fileno].writelnflag = false ; 
  808.  
  809.      sp-=4;
  810. }
  811.  
  812.  
  813. /**********************************************************************/
  814. /*                      P-code   別 処理エントリ表                    */
  815. /**********************************************************************/
  816.  
  817. static struct entry { 
  818.        void (*func)(void) ;
  819. } pcd[] = {
  820.            /*         xx0  xx1  xx2  xx3  xx4  xx5  xx6  xx7  xx8  xx9   */
  821.  
  822.            /*00x*/    GET, PUT, RST, RLN, NEW, WLN, WRS, WRB, WRI, WRR,
  823.            /*01x*/    WRC, RDI, RDR, RDC, SIN, COS, EXP, LOG, SQT, ATN,
  824.            /*02x*/    PGE, EoF, EOL, DIS, WRF, RWT, TRS, TRW, TGT, TPT 
  825.          };
  826.  
  827. /********* callsp() : csp 命令の実行 ***********/
  828. void callsp(void)
  829. {
  830.      pcd[q].func() ;                    /* opに対応した命令を実行     */
  831. }
  832.